﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Rapor
{
    public partial class xrKaufKesimFoyu : DevExpress.XtraReports.UI.XtraReport
    {
        public xrKaufKesimFoyu()
        {
            InitializeComponent();
        }
        public xrKaufKesimFoyu(string idx)
            : this()
        {
            myData1.Merge(Session.DataManager.ExecuteDataSet("GetGenericBasicFoy", idx));
            myData1.xKaufMaster.Merge(myData1.Tables["Table"]);
            myData1.xKaufFoys.Merge(myData1.Tables["Table1"]);
            myData1.KaufKesimNo.Merge(myData1.Tables["Table2"]);
        }

        private DataTable RemoveUnusedColumns(DataTable dt)
        {
            string colNames = "";
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string activeColName = dt.Columns[i].ColumnName;
                if (activeColName.StartsWith("A") && activeColName.Length == 3)
                {
                    int sum = dt.Select("ISNULL([" + activeColName + "],0)<>0").Length;
                    if (sum == 0)
                    {
                        colNames += dt.Columns[i].ColumnName + ";";
                    }
                    else
                        break;
                }
                else
                    colNames += dt.Columns[i].ColumnName + ";";
            }

            for (int i = dt.Columns.Count - 1; i > 0; i--)
            {
                string activeColName = dt.Columns[i].ColumnName;
                if (activeColName.StartsWith("A") && activeColName.Length == 3)
                {
                    int sum = dt.Select("ISNULL([" + activeColName + "],0)<>0").Length;
                    if (sum == 0)
                    {
                        colNames += dt.Columns[i].ColumnName + ";";
                    }
                    else
                        break;
                }
                else
                    colNames += dt.Columns[i].ColumnName + ";";
            }

            foreach (string colName in colNames.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                dt.Columns.Remove(colName);
            }

            return dt;
        }

        private void AddBDColumns(string colName)
        {
            string caption;
            if (colName != "Total")
            {
                DataRow dr = myData1.Tables["Table3"].Select("Name='" + colName + "'")[0];
                caption = dr["Beden"].ToString();
            }

            else
            {
                caption = "Total";
            }
            XRTableCell cell = new XRTableCell();
            cell.Name = "header" + colName;
            cell.Text = caption;//colName.Substring(1, colName.Length - 1);
            cell.WidthF = 20;

            XRTableCell detailCell = new XRTableCell();
            detailCell.Name = "detail" + colName;
            detailCell.DataBindings.AddRange(new DevExpress.XtraReports.UI.XRBinding[] {
            new DevExpress.XtraReports.UI.XRBinding("Text", null, "xKaufMaster.FK_xKaufFoys_xKaufMaster_Style."+colName)});
            detailCell.WidthF = 20;

            XRTableCell footerCell = new XRTableCell();
            detailCell.Name = "footer" + colName;
            footerCell.Text = "";
            footerCell.WidthF = 20;

            XRTableCell sfooterCell = new XRTableCell();
            sfooterCell.Name = "sfooter" + colName;
            sfooterCell.Text = "";
            sfooterCell.WidthF = 20;

            if (colName == "Total")
            {
                cell.Text = "TOTAL";

                cell.Borders = ((DevExpress.XtraPrinting.BorderSide)((((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top)
                    | DevExpress.XtraPrinting.BorderSide.Right)
                    | DevExpress.XtraPrinting.BorderSide.Bottom)));

                detailCell.Borders =
                footerCell.Borders =
                sfooterCell.Borders =
                    ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right)
                    | DevExpress.XtraPrinting.BorderSide.Bottom)));
            }

            trHeader.Cells.Add(cell);
            trDetail.Cells.Add(detailCell);
            trFooter.Cells.Add(footerCell);
            trSfooter.Cells.Add(sfooterCell);

        }

        private void xrDU4KesimFoyu_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            tbHeader.BeginInit();
            tbDetail.BeginInit();
            tbKumas.BeginInit(); // (BS)
            tbFooter.BeginInit();
            tbSfooter.BeginInit();

            DataTable dt = new DataTable();

            dt.Merge(myData1.xKaufFoys);
            DataTable dtDetail = RemoveUnusedDetailColumns(dt);

            for (int i = 0; i < dtDetail.Columns.Count; i++)
            {
                string colName = dtDetail.Columns[i].ColumnName;
                AddDetailColumns(colName);
            }

            DataTable dt2 = new DataTable();
            dt2.Merge(myData1.xKaufFoys);
            DataTable dtBeden = RemoveUnusedColumns(dt2);

            for (int i = 0; i < dtBeden.Columns.Count; i++)
            {
                string colName = dtBeden.Columns[i].ColumnName;
                AddBDColumns(colName);
            }

            AddBDColumns("Total");

            if (myData1.xKaufFoys.Select("ISNULL(Garni,'')<>''").Length == 0)
            {
                headerContrast.Dispose();
                detailContrast.Dispose();
                footerContrast.Dispose();
                sfooterContrast.Dispose();
            }
            if (myData1.xKaufFoys.Select("ISNULL(Garni2,'')<>''").Length == 0)
            {
                headerContrast2.Dispose();
                detailContrast2.Dispose();
                footerContrast2.Dispose();
                sfooterContrast2.Dispose();
            }
            if (myData1.xKaufFoys.Select("ISNULL(OrjArticle,'')<>''").Length == 0)
            {
                headerOrjArticle.Dispose();
                detailOrjArticle.Dispose();
                footerOrjArticle.Dispose();
                SfooterOrjArticle.Dispose();
            }
            if (myData1.xKaufFoys.Select("ISNULL(Order,'')<>''").Length == 0)
            {
                xrTableCell1.Dispose();
                xrTableCell2.Dispose();
                xrTableCell3.Dispose();
                xrTableCell8.Dispose();
            }
            tbHeader.WidthF = 2800;
            tbDetail.WidthF = 2800;
            tbFooter.WidthF = 2800;
            tbSfooter.WidthF = 2800;

            tbHeader.EndInit();
            tbDetail.EndInit();
            tbKumas.EndInit(); // (BS)
            tbFooter.EndInit();
            tbSfooter.EndInit();
        }

        private void AddDetailColumns(string colName)
        {
            string caption = colName.Replace("detail", "");

            XRTableCell cell = new XRTableCell();
            //cell.Name = "header" + colName;
            cell.Text = caption;
            cell.WidthF = 70;

            XRTableCell detailCell = new XRTableCell();
            //detailCell.Name = "StackTrace" + colName;
            detailCell.DataBindings.AddRange(new DevExpress.XtraReports.UI.XRBinding[] {
            new DevExpress.XtraReports.UI.XRBinding("Text", null, "xKaufMaster.FK_xKaufFoys_xKaufMaster_Style."+colName)});
            detailCell.WidthF = 70;

            XRTableCell footerCell = new XRTableCell();
            //detailCell.Name = "footer" + colName;
            footerCell.Text = "";
            footerCell.WidthF = 70;

            XRTableCell sfooterCell = new XRTableCell();
            //sfooterCell.Name = "sfooter" + colName;
            sfooterCell.Text = "";
            sfooterCell.WidthF = 70;

            trHeader.Cells.Add(cell);
            trDetail.Cells.Add(detailCell);
            trFooter.Cells.Add(footerCell);
            trSfooter.Cells.Add(sfooterCell);

        }

        private DataTable RemoveUnusedDetailColumns(DataTable dt)
        {
            string colNames = "";
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string activeColName = dt.Columns[i].ColumnName;
                if (activeColName.StartsWith("detail"))
                {
                    int sum = dt.Select("ISNULL([" + activeColName + "], '' ) <> '' ").Length;
                    if (sum == 0)
                        colNames += dt.Columns[i].ColumnName + ";";
                }
                else
                    colNames += dt.Columns[i].ColumnName + ";";
            }


            foreach (string colName in colNames.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                dt.Columns.Remove(colName);
            }

            return dt;
        }

        private void detailContrast_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            pbGarni1.WidthF =
            xrTable6.WidthF = detailContrast.WidthF;
        }

        private void detailContrast2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrTable10.WidthF =
            pbGarni2.WidthF = detailContrast2.WidthF;
        }

        private void detailKumas_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            tbKumas.WidthF = detailKumas.WidthF;
            pbKumas.WidthF = detailKumas.WidthF;
            pbKumas.LocationF = new PointF(detailKumas.LocationF.X + ((detailKumas.WidthF - pbKumas.WidthF) / 2), pbKumas.LocationF.Y);
        }

    }
}
